from conan import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain, CMakeDeps
from conan.tools.files import copy
from conan.tools.build import check_min_cppstd
import os


class HelloConan(ConanFile):
    """一个简单的C++库示例"""

    # 基本信息
    name = "hello"
    version = "1.0.0"
    description = "A simple C++ library example"
    license = "MIT"
    homepage = "https://example.com/hello"
    url = "https://github.com/example/hello"

    # 设置
    settings = "os", "compiler", "build_type", "arch", "cppstd"

    # 选项
    options = {"shared": [True, False]}
    default_options = {"shared": False}

    # 依赖
    requires = ["boost/1.80.0", "fmt/9.1.0"]
    tool_requires = ["cmake/3.24.0"]

    # 导出文件
    exports_sources = ["src/*", "include/*", "CMakeLists.txt"]

    # 生成器
    generators = ["CMakeDeps", "CMakeToolchain"]

    def configure(self):
        """配置验证"""
        if self.settings.compiler.cppstd:
            check_min_cppstd(self, "14")

        if self.options.shared:
            self.options.rm_safe("fPIC")
            self.options["fPIC"] = True

    def generate(self):
        """生成构建文件"""
        tc = CMakeToolchain(self)
        tc.generate()

        deps = CMakeDeps(self)
        deps.generate()

    def build(self):
        """构建项目"""
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def package(self):
        """打包文件"""
        cmake = CMake(self)
        cmake.install()

        # 手动复制头文件
        copy(self, "*.h", src=os.path.join(self.source_folder, "include"),
             dst=os.path.join(self.package_folder, "include"))

    def package_info(self):
        """包信息"""
        self.cpp_info.libs = ["hello"]
        self.cpp_info.includedirs = ["include"]

        if self.settings.build_type == "Debug":
            self.cpp_info.defines = ["HELLO_DEBUG"]
